// pages/buy/buy.js
const app = getApp()
var baseurl = app.baseurl.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag1: '',
    flag: true,
    buy: '',
    notice: '',//预约须知
    prizeid: '',
    checked: '',
    disable: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.request({
      url: `${baseurl}api/Prize/prizeList`,
      method: 'POST',
      data: {},
      success: (res) => {
        this.setData({
          buy: res.data.data
        })
      }
    })
    // 须知
    wx.request({
      url: `${baseurl}api/system/mustKnow`,
      method: 'POST',
      data: {},
      success: (res) => {
        // console.log(res.data.data);
        this.setData({
          notice: res.data.data
        })
      }
    })
  },
  onbox() {
    this.setData({
      flag: true
    })
  },
  buy(id) {
    // console.log(id.currentTarget.dataset.cap);
    // 保存商品id
    wx.setStorageSync('prizeid', id.currentTarget.dataset.cap)
    this.setData({
      flag: false,
      prizeid: id.currentTarget.dataset.cap
    })
  },

  // 复选框是否勾选
  checkchange(e) {
    // console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    const checked = e.detail.value
    // console.log(checked);
    this.setData({
      checked
    })
  },

  // 预约须知确定按钮
  sure() {
    // this.setData({
    //   flag: true
    // }),
    var that = this
    let openid = wx.getStorageSync("openid")
    // console.log(wx.getStorageSync("openid"));
    // 是否有openid登录
    if (openid != '') {
      this.setData({
        flag: true
      })
      // console.log(this.data.checked == '');
      if (this.data.checked == '') {
        // console.log('未选中');
        wx.showToast({
          title: '请阅读并同意预约须知',
          icon: 'none',
          duration: 2000
        })
      } else {
        // console.log('已选中');
        wx.request({
          url: `${baseurl}api/Login/userinfo`,
          method: 'POST',
          data: {
            id: wx.getStorageSync("id"),
          },
          success(res) {
            // console.log(res.data.data.tel);
            if (res.data.data.tel == null) {
              wx.navigateTo({
                url: '/pages/message/message'
              })
            } else {
              wx.request({
                url: `${baseurl}api/Reservation/reserinfo`,
                method: 'POST',
                data: {
                  openid: wx.getStorageSync("openid"),
                  user_id: wx.getStorageSync("id"),
                  prizeid: that.data.prizeid

                },
                success(res) {
                  that.setData({
                    disable: true
                  })
                  // console.log(that.data.disable);
                  // console.log(res);
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 2000
                  })
                  setTimeout(
                    function () {
                      wx.reLaunch({
                        url:
                          '/pages/order/order',
                      })

                    },
                    2000)

                }

              })



            }
          }
        })

      }

      // console.log(111);
    } else {
      wx.navigateTo({
        url: '/pages/login/login'
      })
      // console.log(222);
    }


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})